package org.steven.crm.core.service.system;

import org.steven.crm.core.query.SystemConfigQueryDataList;
import org.steven.crm.core.service.BaseService;
import com.steven.xdb.dbmanager.DBManager;
import com.steven.xdb.pagination.query.AbstractQueryDataList;
import com.steven.xdb.session.DBSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author wuzw
 * @Date 2021/5/24 21:30
 * @Version 1.0
 */
public class SystemConfigService extends BaseService {
    @Override
    protected String getTable() {
        return "systemConfig";
    }

    @Override
    protected AbstractQueryDataList getQueryDataList() {
        return new SystemConfigQueryDataList();
    }

    @Override
    protected String getDataSourceName() {
        return null;
    }

    public Map<String, String> readAll() throws Exception {
        Map<String, String> result = new HashMap<>();
        DBSession session = getSession();
        List<Map<String, String>> datas = DBManager.query(session, getTable(), new HashMap<>());
        for (Map<String, String> data : datas) {
            result.put(data.get("id"), data.get("value"));
        }

        return result;
    }

    public String getValue(String id) throws Exception {
        Map<String, String> data = this.read(id);
        return data.get("value");
    }

    public Map<String, String> readAllPublic() throws Exception {
        Map<String, String> result = new HashMap<>();
        DBSession session = getSession();
        Map<String, String> k = new HashMap<>();
        k.put("publicFlag", "1");
        List<Map<String, String>> datas = DBManager.query(session, getTable(), k);
        for (Map<String, String> data : datas) {
            result.put(data.get("id"), data.get("value"));
        }

        return result;
    }
}
